// import {
// 	login,
// 	getUserInfo
// } from '@/api/index'




export function phoneReg(phone) {
	var myreg = /^[1][3-9][0-9]{9}$/;
	return myreg.test(phone)
}

export function initLogin(phoneNumber) {
	return new Promise((resolve) => {
		uni.showLoading({
			title: '',
			mask: true
		});
		uni.login({
			provider: 'weixin', //使用微信登录
			success: async function(loginRes) {
				login({
					js_code: loginRes.code,
          phoneNumber,
				}).then(res => {
					uni.hideLoading();
					if (res.result) {
						uni.setStorageSync('token', res.data)
						uni.setStorageSync('openid', res.object.openid)
						uni.showLoading({
							title: '',
							mask: true
						});
						getUserInfo({openId: uni.getStorageSync('openid')}).then(res => {
							uni.hideLoading();
							if (res.result) {
								const info = {
									id: res.object.id,
									phone: res.object.phone,
									headPicture: res.object.headPicture || '',
									gender: res.object.gender || '',
									nickName: res.object.nickName || '',
								}
								uni.setStorageSync('userInfo', info)
							}
							resolve()
						})
					} else {
						uni.showToast({
							title: res.msg,
							icon: 'none'
						});
						resolve()
					}
				})
			}
		});
	})
}


